import {
  userList,
  userListByGroup,
  userListByGID
} from '../services/TableList';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'TableList',

  state: {
    dataList: [],
    condition: '',
    pagination: {
      current: 1,
      pageSize: 10,
      total: 0
    },
    loading: false
  },

  subscriptions: {},

  effects: { * userList({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'showLoading'
      });

      const {
        data
      } = yield call(userList, parse(payload));

      if (data) {
        yield put({
          type: 'userListSuccess',
          payload: {
            dataList: data.data && data.data.detail || [],
            pagination: {
              current: parseInt(data.data && data.data.page || 1),
              pageSize: parseInt(data.data && data.data.size || 10),
              total: data.data && data.data.totalRecordSize || 0
            },
          },
        });
      } else {
        error('获取用户列表失败');
      }
    },
    //====================================
    * userListByGroup({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'showLoading'
      });

      let params = parse(payload);

      const {
        data
      } = yield call(userListByGroup, params);

      if (data) {
        yield put({
          type: 'userListSuccess',
          payload: {
            dataList: data.data && data.data.detail || [],
            condition: params.condition,
            channelType: params.channelType,
            pagination: {
              current: parseInt(data.data && data.data.page || 1),
              pageSize: parseInt(data.data && data.data.size || 10),
              total: parseInt(data.data && data.data.totalRecordSize || 0)
            },
          },
        });
      } else {
        error('根据群组获取用户列表失败');
      }
    },
    //====================================
    * userListByGID({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'showLoading'
      });

      let params = parse(payload);

      const {
        data
      } = yield call(userListByGID, params);

      if (data) {
        yield put({
          type: 'userListSuccess',
          payload: {
            dataList: data.data && data.data.detail || [],
            condition: params.condition,
            channelType: params.channelType,
            pagination: {
              current: parseInt(data.data && data.data.page || 1),
              pageSize: parseInt(data.data && data.data.size || 10),
              total: data.data && data.data.totalRecordSize || 0
            },
          },
        });
      } else {
        error('根据用户自定义群组id查询用户列表失败');
      }
    },

  },

  reducers: {
    showLoading(state) {
      return {...state,
        loading: true
      };
    },
    userListSuccess(state, action) {
      return {...state,
        ...action.payload,
        loading: false
      };
    },
  },
};